
*************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************

clear
set matsize 2000

*************************************************************************************************************************************************************************
**** --> MARGINAL EFFECTS - STANDARDISED INDEPENDENT VARIABLES **********************************************************************************************************
*************************************************************************************************************************************************************************

****************************************************************************************************************
**** PARTY-LEVEL MODEL: MI DATA + PARTY CONTROLS ***************************************************************
****************************************************************************************************************

use TimelinePartiesMI_New.dta, clear

*** STANDARDISE THE INDEPENDENT VARIABLES **********************************************************************

egen std_pr=std(pr)
egen std_enpp=std(enpp)
egen std_partycentric=std(partycentric)
egen std_vote=std(vote_)
egen std_partyold=std(partyold)
egen std_niche=std(niche)
egen std_gov=std(gov_)
egen std_inc=std(inc_)

replace pr=std_pr
replace enpp=std_enpp
replace partycentric=std_partycentric 
replace vote=std_vote 	
replace partyold=std_partyold
replace niche=std_niche 
replace gov=std_gov 	
replace inc=std_inc 	

replace t_pr=pr*daysbeforeED_0_001
replace t_enpp=enpp*daysbeforeED_0_001
replace t_partycentric=partycentric*daysbeforeED_0_001
replace t_vote=vote*daysbeforeED_0_001
replace t_partyold=partyold*daysbeforeED_0_001 
replace t_niche=niche*daysbeforeED_0_001
replace t_gov=gov*daysbeforeED_0_001
replace t_inc=inc*daysbeforeED_0_001 

****************************************************************************************************************

  gen estimate=.
  gen stderror=.
  gen n=.
  gen min95=.
  gen max95=.
  
  
local agrp "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365"
local bgrp "1.17E-09 0.1419616 0.2248023 0.2834263 0.3287767 0.3657292 0.3968855 0.4237985 0.4474702 0.468585 0.4876307 0.5049679 0.5208701 0.5355504 0.5491773 0.5618868 0.5737901 0.5849797 0.5955325 0.6055138 0.6149793 0.6239773 0.6325493 0.6407312 0.6485552 0.6560493 0.6632382 0.6701443 0.6767874 0.6831853 0.6893542 0.6953083 0.7010612 0.7066249 0.7120099 0.7172266 0.7222844 0.7271914 0.7319558 0.7365844 0.7410842 0.7454613 0.7497215 0.7538702 0.7579125 0.7618527 0.7656955 0.769445 0.7731051 0.7766792 0.7801707 0.7835831 0.7869191 0.7901816 0.7933736 0.7964973 0.7995552 0.8025498 0.805483 0.8083567 0.8111735 0.8139346 0.816642 0.8192976 0.8219028 0.8244591 0.8269681 0.8294312 0.8318496 0.8342248 0.8365577 0.8388499 0.8411022 0.8433158 0.8454917 0.847631 0.8497345 0.8518032 0.853838 0.8558397 0.8578092 0.8597473 0.8616548 0.8635322 0.8653804 0.8672 0.8689917 0.8707563 0.8724942 0.8742059 0.8758923 0.8775536 0.8791909 0.880804 0.8823937 0.8839608 0.8855053 0.887028 0.8885291 0.8900093 0.8914688 0.892908 0.8943275 0.8957275 0.8971082 0.8984703 0.8998142 0.9011399 0.9024481 0.9037387 0.9050125 0.9062693 0.9075098 0.9087342 0.9099426 0.9111354 0.9123128 0.9134752 0.9146227 0.9157558 0.9168745 0.9179791 0.9190698 0.9201468 0.9212106 0.9222609 0.9232984 0.924323 0.925335 0.9263347 0.9273221 0.9282975 0.929261 0.930213 0.9311535 0.9320824 0.9330003 0.9339072 0.9348033 0.9356887 0.9365636 0.9374281 0.9382823 0.9391264 0.9399605 0.940785 0.9415997 0.9424049 0.9432005 0.9439868 0.9447642 0.9455323 0.9462915 0.947042 0.9477836 0.9485168 0.9492414 0.9499577 0.9506657 0.9513656 0.9520574 0.9527411 0.9534172 0.9540852 0.9547458 0.9553988 0.9560441 0.9566822 0.9573128 0.9579363 0.9585527 0.9591619 0.9597642 0.9603597 0.9609482 0.96153 0.9621053 0.9626738 0.9632359 0.9637915 0.9643407 0.9648838 0.9654204 0.9659509 0.9664754 0.9669937 0.9675063 0.9680128 0.9685135 0.9690085 0.9694977 0.9699812 0.9704592 0.9709315 0.9713986 0.97186 0.972316 0.972767 0.9732125 0.9736528 0.9740878 0.9745179 0.9749429 0.9753629 0.9757778 0.976188 0.9765931 0.9769936 0.9773891 0.9777802 0.9781662 0.9785479 0.978925 0.9792972 0.9796653 0.9800287 0.9803878 0.9807425 0.9810928 0.9814386 0.9817805 0.982118 0.9824513 0.9827804 0.9831055 0.9834265 0.9837433 0.9840563 0.9843653 0.9846701 0.9849714 0.9852685 0.985562 0.9858516 0.9861373 0.9864194 0.9866978 0.9869725 0.9872437 0.987511 0.9877748 0.9880353 0.9882919 0.9885452 0.988795 0.9890414 0.9892843 0.9895238 0.98976 0.989993 0.9902225 0.9904488 0.9906718 0.9908916 0.9911083 0.9913216 0.991532 0.9917391 0.9919432 0.9921442 0.9923421 0.992537 0.9927288 0.9929178 0.9931037 0.9932867 0.9934667 0.9936439 0.993818 0.9939895 0.9941583 0.9943241 0.994487 0.9946471 0.9948048 0.9949595 0.9951115 0.9952608 0.9954075 0.9955516 0.9956929 0.9958317 0.9959678 0.9961014 0.9962325 0.996361 0.996487 0.9966103 0.9967313 0.9968498 0.9969658 0.9970796 0.9971907 0.9972994 0.9974059 0.9975098 0.9976115 0.9977108 0.9978077 0.9979025 0.9979948 0.9980849 0.9981728 0.9982582 0.9983416 0.9984228 0.9985017 0.9985784 0.9986529 0.9987254 0.9987957 0.9988637 0.9989297 0.9989935 0.9990553 0.999115 0.9991725 0.9992282 0.9992816 0.9993331 0.9993827 0.9994302 0.9994757 0.9995192 0.9995607 0.9996003 0.999638 0.9996735 0.9997075 0.9997393 0.9997692 0.9997973 0.9998235 0.9998479 0.9998704 0.999891 0.9999099 0.9999269 0.9999422 0.9999555 0.9999671 0.9999769 0.9999851 0.9999914 0.9999959 0.9999987 0.9999997 0.9999993 0.999997 0.9999928 0.9999872 0.9999799 0.9999709 0.9999602 0.9999478 0.9999338 0.9999181 0.9999009 0.999882 0.9998615 0.9998395 0.9998159 0.9997908 0.999764 0.9997357"

local n : word count `agrp'

mi estimate, post dots: reg log_ae daysbeforeED_0_001 ipollse enpp t_enpp pr t_pr vote t_vote partyold t_partyold niche t_niche gov t_gov inc t_inc

forvalues i = 1/`n' {

  local a : word `i' of `agrp'
  local b : word `i' of `bgrp'
 
 {
  nlcom (_b[enpp])+(_b[t_enpp]*(`b'))

  matrix b = r(b)
  scalar bt = b[1,1]
  replace estimate=bt in `a'
  matrix b = r(V)
  scalar bt = b[1,1]
  replace stderror=sqrt(bt) in `a'
  replace n=`a' in `a'
  }
  
}

replace min95=estimate-stderror
replace max95=estimate+stderror

keep n estimate min95 max95
drop if estimate==.
  
gen count=_n

format estimate min95 max95 %8.1f

rename estimate ae
rename min95 ae_min95
rename max95 ae_max95

sort count

gen aeraw=exp(ae)
replace aeraw=aeraw-1

gen aerawmin95=exp(ae_min95)
replace aerawmin95=aerawmin95-1

gen aeraw_max95=exp(ae_max95)
replace aeraw_max95=aeraw_max95-1

save MarginsNL_enpp.dta, replace

use MarginsNL_enpp.dta, clear

* 
twoway line aeraw count, /*
*/ clpattern(solid) clcolor(gs0) clwidth(medthick)  /* 
*/ || line aerawmin95 count,  /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/ || line aeraw_max95 count, /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/  , scheme(plottig) graphregion(color(white)) /*
*/  xtitle("Days until election") /*
*/  title("Effective number of parties", size(small)) /*
*/  ylabel(-0.2(0.1).2, labsize(small) gmax angle(horizontal)) /*
*/  ytick(-0.2(0.1).2) /*
*/  yscale(r(-.2 .2) titlegap(*5)) /*
*/  yline(0, lcolor(red) lpattern(solid)) /*
*/  xlabel(0(100)400, labsize(small)) /*
*/  xscale(reverse) xscale(titlegap(3)) /*
*/  legend(off)  /*
*/  saving(MarginsNL_1_enpp.gph, replace)


*************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************

clear
set matsize 2000

*************************************************************************************************************************************************************************
**** --> MARGINAL EFFECTS - STANDARDISED INDEPENDENT VARIABLES **********************************************************************************************************
*************************************************************************************************************************************************************************

****************************************************************************************************************
**** PARTY-LEVEL MODEL: MI DATA + PARTY CONTROLS ***************************************************************
****************************************************************************************************************

use TimelinePartiesMI_New.dta, clear

*** STANDARDISE THE INDEPENDENT VARIABLES **********************************************************************

egen std_pr=std(pr)
egen std_enpp=std(enpp)
egen std_partycentric=std(partycentric)
egen std_vote=std(vote_)
egen std_partyold=std(partyold)
egen std_niche=std(niche)
egen std_gov=std(gov_)
egen std_inc=std(inc_)

replace pr=std_pr
replace enpp=std_enpp
replace partycentric=std_partycentric 
replace vote=std_vote 	
replace partyold=std_partyold
replace niche=std_niche 
replace gov=std_gov 	
replace inc=std_inc 	

replace t_pr=pr*daysbeforeED_0_001
replace t_enpp=enpp*daysbeforeED_0_001
replace t_partycentric=partycentric*daysbeforeED_0_001
replace t_vote=vote*daysbeforeED_0_001
replace t_partyold=partyold*daysbeforeED_0_001 
replace t_niche=niche*daysbeforeED_0_001
replace t_gov=gov*daysbeforeED_0_001
replace t_inc=inc*daysbeforeED_0_001 

****************************************************************************************************************

  gen estimate=.
  gen stderror=.
  gen n=.
  gen min95=.
  gen max95=.
  
local agrp "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365"
local bgrp "1.17E-09 0.1419616 0.2248023 0.2834263 0.3287767 0.3657292 0.3968855 0.4237985 0.4474702 0.468585 0.4876307 0.5049679 0.5208701 0.5355504 0.5491773 0.5618868 0.5737901 0.5849797 0.5955325 0.6055138 0.6149793 0.6239773 0.6325493 0.6407312 0.6485552 0.6560493 0.6632382 0.6701443 0.6767874 0.6831853 0.6893542 0.6953083 0.7010612 0.7066249 0.7120099 0.7172266 0.7222844 0.7271914 0.7319558 0.7365844 0.7410842 0.7454613 0.7497215 0.7538702 0.7579125 0.7618527 0.7656955 0.769445 0.7731051 0.7766792 0.7801707 0.7835831 0.7869191 0.7901816 0.7933736 0.7964973 0.7995552 0.8025498 0.805483 0.8083567 0.8111735 0.8139346 0.816642 0.8192976 0.8219028 0.8244591 0.8269681 0.8294312 0.8318496 0.8342248 0.8365577 0.8388499 0.8411022 0.8433158 0.8454917 0.847631 0.8497345 0.8518032 0.853838 0.8558397 0.8578092 0.8597473 0.8616548 0.8635322 0.8653804 0.8672 0.8689917 0.8707563 0.8724942 0.8742059 0.8758923 0.8775536 0.8791909 0.880804 0.8823937 0.8839608 0.8855053 0.887028 0.8885291 0.8900093 0.8914688 0.892908 0.8943275 0.8957275 0.8971082 0.8984703 0.8998142 0.9011399 0.9024481 0.9037387 0.9050125 0.9062693 0.9075098 0.9087342 0.9099426 0.9111354 0.9123128 0.9134752 0.9146227 0.9157558 0.9168745 0.9179791 0.9190698 0.9201468 0.9212106 0.9222609 0.9232984 0.924323 0.925335 0.9263347 0.9273221 0.9282975 0.929261 0.930213 0.9311535 0.9320824 0.9330003 0.9339072 0.9348033 0.9356887 0.9365636 0.9374281 0.9382823 0.9391264 0.9399605 0.940785 0.9415997 0.9424049 0.9432005 0.9439868 0.9447642 0.9455323 0.9462915 0.947042 0.9477836 0.9485168 0.9492414 0.9499577 0.9506657 0.9513656 0.9520574 0.9527411 0.9534172 0.9540852 0.9547458 0.9553988 0.9560441 0.9566822 0.9573128 0.9579363 0.9585527 0.9591619 0.9597642 0.9603597 0.9609482 0.96153 0.9621053 0.9626738 0.9632359 0.9637915 0.9643407 0.9648838 0.9654204 0.9659509 0.9664754 0.9669937 0.9675063 0.9680128 0.9685135 0.9690085 0.9694977 0.9699812 0.9704592 0.9709315 0.9713986 0.97186 0.972316 0.972767 0.9732125 0.9736528 0.9740878 0.9745179 0.9749429 0.9753629 0.9757778 0.976188 0.9765931 0.9769936 0.9773891 0.9777802 0.9781662 0.9785479 0.978925 0.9792972 0.9796653 0.9800287 0.9803878 0.9807425 0.9810928 0.9814386 0.9817805 0.982118 0.9824513 0.9827804 0.9831055 0.9834265 0.9837433 0.9840563 0.9843653 0.9846701 0.9849714 0.9852685 0.985562 0.9858516 0.9861373 0.9864194 0.9866978 0.9869725 0.9872437 0.987511 0.9877748 0.9880353 0.9882919 0.9885452 0.988795 0.9890414 0.9892843 0.9895238 0.98976 0.989993 0.9902225 0.9904488 0.9906718 0.9908916 0.9911083 0.9913216 0.991532 0.9917391 0.9919432 0.9921442 0.9923421 0.992537 0.9927288 0.9929178 0.9931037 0.9932867 0.9934667 0.9936439 0.993818 0.9939895 0.9941583 0.9943241 0.994487 0.9946471 0.9948048 0.9949595 0.9951115 0.9952608 0.9954075 0.9955516 0.9956929 0.9958317 0.9959678 0.9961014 0.9962325 0.996361 0.996487 0.9966103 0.9967313 0.9968498 0.9969658 0.9970796 0.9971907 0.9972994 0.9974059 0.9975098 0.9976115 0.9977108 0.9978077 0.9979025 0.9979948 0.9980849 0.9981728 0.9982582 0.9983416 0.9984228 0.9985017 0.9985784 0.9986529 0.9987254 0.9987957 0.9988637 0.9989297 0.9989935 0.9990553 0.999115 0.9991725 0.9992282 0.9992816 0.9993331 0.9993827 0.9994302 0.9994757 0.9995192 0.9995607 0.9996003 0.999638 0.9996735 0.9997075 0.9997393 0.9997692 0.9997973 0.9998235 0.9998479 0.9998704 0.999891 0.9999099 0.9999269 0.9999422 0.9999555 0.9999671 0.9999769 0.9999851 0.9999914 0.9999959 0.9999987 0.9999997 0.9999993 0.999997 0.9999928 0.9999872 0.9999799 0.9999709 0.9999602 0.9999478 0.9999338 0.9999181 0.9999009 0.999882 0.9998615 0.9998395 0.9998159 0.9997908 0.999764 0.9997357"

local n : word count `agrp'

mi estimate, post dots: reg log_ae daysbeforeED_0_001 ipollse enpp t_enpp pr t_pr vote t_vote partyold t_partyold niche t_niche gov t_gov inc t_inc

forvalues i = 1/`n' {
  local a : word `i' of `agrp'
  local b : word `i' of `bgrp'
  
{ 
  nlcom (_b[pr])+(_b[t_pr]*(`b'))

  matrix b = r(b)
  scalar bt = b[1,1]
  replace estimate=bt in `a'
  matrix b = r(V)
  scalar bt = b[1,1]
  replace stderror=sqrt(bt) in `a'
  replace n=`a' in `a'
} 
}

replace min95=estimate-stderror
replace max95=estimate+stderror

keep n estimate min95 max95
drop if estimate==.
  
gen count=_n

format estimate min95 max95 %8.1f

rename estimate ae
rename min95 ae_min95
rename max95 ae_max95

sort count

gen aeraw=exp(ae)
replace aeraw=aeraw-1

gen aerawmin95=exp(ae_min95)
replace aerawmin95=aerawmin95-1

gen aeraw_max95=exp(ae_max95)
replace aeraw_max95=aeraw_max95-1

save MarginsNL_pr.dta, replace

use MarginsNL_pr.dta, clear

* 
twoway line aeraw count, /*
*/ clpattern(solid) clcolor(gs0) clwidth(medthick)  /* 
*/ || line aerawmin95 count,  /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/ || line aeraw_max95 count, /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/  , scheme(plottig) graphregion(color(white)) /*
*/  xtitle("Days until election") /*
*/  title("PR", size(small)) /*
*/  ylabel(-0.2(0.1).2, labsize(small) gmax angle(horizontal)) /*
*/  ytick(-0.2(0.1).2) /*
*/  yscale(r(-.2 .2) titlegap(*5)) /*
*/  yline(0, lcolor(red) lpattern(solid)) /*
*/  xlabel(0(100)400, labsize(small)) /*
*/  xscale(reverse) xscale(titlegap(3)) /*
*/  legend(off)  /*
*/  saving(MarginsNL_2_pr.gph, replace)


*************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************

clear
set matsize 2000

*************************************************************************************************************************************************************************
**** --> MARGINAL EFFECTS - STANDARDISED INDEPENDENT VARIABLES **********************************************************************************************************
*************************************************************************************************************************************************************************

****************************************************************************************************************
**** PARTY-LEVEL MODEL: MI DATA + PARTY CONTROLS ***************************************************************
****************************************************************************************************************

use TimelinePartiesMI_New.dta, clear

*** STANDARDISE THE INDEPENDENT VARIABLES **********************************************************************

egen std_pr=std(pr)
egen std_enpp=std(enpp)
egen std_partycentric=std(partycentric)
egen std_vote=std(vote_)
egen std_partyold=std(partyold)
egen std_niche=std(niche)
egen std_gov=std(gov_)
egen std_inc=std(inc_)

replace pr=std_pr
replace enpp=std_enpp
replace partycentric=std_partycentric 
replace vote=std_vote 	
replace partyold=std_partyold
replace niche=std_niche 
replace gov=std_gov 	
replace inc=std_inc 	

replace t_pr=pr*daysbeforeED_0_001
replace t_enpp=enpp*daysbeforeED_0_001
replace t_partycentric=partycentric*daysbeforeED_0_001
replace t_vote=vote*daysbeforeED_0_001
replace t_partyold=partyold*daysbeforeED_0_001 
replace t_niche=niche*daysbeforeED_0_001
replace t_gov=gov*daysbeforeED_0_001
replace t_inc=inc*daysbeforeED_0_001 

****************************************************************************************************************

  gen estimate=.
  gen stderror=.
  gen n=.
  gen min95=.
  gen max95=.
  
local agrp "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365"
local bgrp "1.17E-09 0.1419616 0.2248023 0.2834263 0.3287767 0.3657292 0.3968855 0.4237985 0.4474702 0.468585 0.4876307 0.5049679 0.5208701 0.5355504 0.5491773 0.5618868 0.5737901 0.5849797 0.5955325 0.6055138 0.6149793 0.6239773 0.6325493 0.6407312 0.6485552 0.6560493 0.6632382 0.6701443 0.6767874 0.6831853 0.6893542 0.6953083 0.7010612 0.7066249 0.7120099 0.7172266 0.7222844 0.7271914 0.7319558 0.7365844 0.7410842 0.7454613 0.7497215 0.7538702 0.7579125 0.7618527 0.7656955 0.769445 0.7731051 0.7766792 0.7801707 0.7835831 0.7869191 0.7901816 0.7933736 0.7964973 0.7995552 0.8025498 0.805483 0.8083567 0.8111735 0.8139346 0.816642 0.8192976 0.8219028 0.8244591 0.8269681 0.8294312 0.8318496 0.8342248 0.8365577 0.8388499 0.8411022 0.8433158 0.8454917 0.847631 0.8497345 0.8518032 0.853838 0.8558397 0.8578092 0.8597473 0.8616548 0.8635322 0.8653804 0.8672 0.8689917 0.8707563 0.8724942 0.8742059 0.8758923 0.8775536 0.8791909 0.880804 0.8823937 0.8839608 0.8855053 0.887028 0.8885291 0.8900093 0.8914688 0.892908 0.8943275 0.8957275 0.8971082 0.8984703 0.8998142 0.9011399 0.9024481 0.9037387 0.9050125 0.9062693 0.9075098 0.9087342 0.9099426 0.9111354 0.9123128 0.9134752 0.9146227 0.9157558 0.9168745 0.9179791 0.9190698 0.9201468 0.9212106 0.9222609 0.9232984 0.924323 0.925335 0.9263347 0.9273221 0.9282975 0.929261 0.930213 0.9311535 0.9320824 0.9330003 0.9339072 0.9348033 0.9356887 0.9365636 0.9374281 0.9382823 0.9391264 0.9399605 0.940785 0.9415997 0.9424049 0.9432005 0.9439868 0.9447642 0.9455323 0.9462915 0.947042 0.9477836 0.9485168 0.9492414 0.9499577 0.9506657 0.9513656 0.9520574 0.9527411 0.9534172 0.9540852 0.9547458 0.9553988 0.9560441 0.9566822 0.9573128 0.9579363 0.9585527 0.9591619 0.9597642 0.9603597 0.9609482 0.96153 0.9621053 0.9626738 0.9632359 0.9637915 0.9643407 0.9648838 0.9654204 0.9659509 0.9664754 0.9669937 0.9675063 0.9680128 0.9685135 0.9690085 0.9694977 0.9699812 0.9704592 0.9709315 0.9713986 0.97186 0.972316 0.972767 0.9732125 0.9736528 0.9740878 0.9745179 0.9749429 0.9753629 0.9757778 0.976188 0.9765931 0.9769936 0.9773891 0.9777802 0.9781662 0.9785479 0.978925 0.9792972 0.9796653 0.9800287 0.9803878 0.9807425 0.9810928 0.9814386 0.9817805 0.982118 0.9824513 0.9827804 0.9831055 0.9834265 0.9837433 0.9840563 0.9843653 0.9846701 0.9849714 0.9852685 0.985562 0.9858516 0.9861373 0.9864194 0.9866978 0.9869725 0.9872437 0.987511 0.9877748 0.9880353 0.9882919 0.9885452 0.988795 0.9890414 0.9892843 0.9895238 0.98976 0.989993 0.9902225 0.9904488 0.9906718 0.9908916 0.9911083 0.9913216 0.991532 0.9917391 0.9919432 0.9921442 0.9923421 0.992537 0.9927288 0.9929178 0.9931037 0.9932867 0.9934667 0.9936439 0.993818 0.9939895 0.9941583 0.9943241 0.994487 0.9946471 0.9948048 0.9949595 0.9951115 0.9952608 0.9954075 0.9955516 0.9956929 0.9958317 0.9959678 0.9961014 0.9962325 0.996361 0.996487 0.9966103 0.9967313 0.9968498 0.9969658 0.9970796 0.9971907 0.9972994 0.9974059 0.9975098 0.9976115 0.9977108 0.9978077 0.9979025 0.9979948 0.9980849 0.9981728 0.9982582 0.9983416 0.9984228 0.9985017 0.9985784 0.9986529 0.9987254 0.9987957 0.9988637 0.9989297 0.9989935 0.9990553 0.999115 0.9991725 0.9992282 0.9992816 0.9993331 0.9993827 0.9994302 0.9994757 0.9995192 0.9995607 0.9996003 0.999638 0.9996735 0.9997075 0.9997393 0.9997692 0.9997973 0.9998235 0.9998479 0.9998704 0.999891 0.9999099 0.9999269 0.9999422 0.9999555 0.9999671 0.9999769 0.9999851 0.9999914 0.9999959 0.9999987 0.9999997 0.9999993 0.999997 0.9999928 0.9999872 0.9999799 0.9999709 0.9999602 0.9999478 0.9999338 0.9999181 0.9999009 0.999882 0.9998615 0.9998395 0.9998159 0.9997908 0.999764 0.9997357"

local n : word count `agrp'

mi estimate, post dots: reg log_ae daysbeforeED_0_001 ipollse enpp t_enpp pr t_pr vote t_vote partyold t_partyold niche t_niche gov t_gov inc t_inc

forvalues i = 1/`n' {
  local a : word `i' of `agrp'
  local b : word `i' of `bgrp'
  
{ 
  nlcom (_b[vote])+(_b[t_vote]*(`b'))

  matrix b = r(b)
  scalar bt = b[1,1]
  replace estimate=bt in `a'
  matrix b = r(V)
  scalar bt = b[1,1]
  replace stderror=sqrt(bt) in `a'
  replace n=`a' in `a'
}
}

replace min95=estimate-stderror
replace max95=estimate+stderror

keep n estimate min95 max95
drop if estimate==.
  
gen count=_n

format estimate min95 max95 %8.1f

rename estimate ae
rename min95 ae_min95
rename max95 ae_max95

sort count

gen aeraw=exp(ae)
replace aeraw=aeraw-1

gen aerawmin95=exp(ae_min95)
replace aerawmin95=aerawmin95-1

gen aeraw_max95=exp(ae_max95)
replace aeraw_max95=aeraw_max95-1

save MarginsNL_partysize.dta, replace

use MarginsNL_partysize.dta, clear

* 
twoway line aeraw count, /*
*/ clpattern(solid) clcolor(gs0) clwidth(medthick)  /* 
*/ || line aerawmin95 count,  /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/ || line aeraw_max95 count, /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/  , scheme(plottig) graphregion(color(white)) /*
*/  xtitle("Days until election") /*
*/  title("Party size", size(small)) /*
*/  ylabel(-0.2(0.1).2, labsize(small) gmax angle(horizontal)) /*
*/  ytick(-0.2(0.1).2) /*
*/  yscale(r(-.2 .2) titlegap(*5)) /*
*/  yline(0, lcolor(red) lpattern(solid)) /*
*/  xlabel(0(100)400, labsize(small)) /*
*/  xscale(reverse) xscale(titlegap(3)) /*
*/  legend(off)  /*
*/  saving(MarginsNL_3_partysize.gph, replace)


*************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************

clear
set matsize 2000

*************************************************************************************************************************************************************************
**** --> MARGINAL EFFECTS - STANDARDISED INDEPENDENT VARIABLES **********************************************************************************************************
*************************************************************************************************************************************************************************

****************************************************************************************************************
**** PARTY-LEVEL MODEL: MI DATA + PARTY CONTROLS ***************************************************************
****************************************************************************************************************

use TimelinePartiesMI_New.dta, clear

*** STANDARDISE THE INDEPENDENT VARIABLES **********************************************************************

egen std_pr=std(pr)
egen std_enpp=std(enpp)
egen std_partycentric=std(partycentric)
egen std_vote=std(vote_)
egen std_partyold=std(partyold)
egen std_niche=std(niche)
egen std_gov=std(gov_)
egen std_inc=std(inc_)

replace pr=std_pr
replace enpp=std_enpp
replace partycentric=std_partycentric 
replace vote=std_vote 	
replace partyold=std_partyold
replace niche=std_niche 
replace gov=std_gov 	
replace inc=std_inc 	

replace t_pr=pr*daysbeforeED_0_001
replace t_enpp=enpp*daysbeforeED_0_001
replace t_partycentric=partycentric*daysbeforeED_0_001
replace t_vote=vote*daysbeforeED_0_001
replace t_partyold=partyold*daysbeforeED_0_001 
replace t_niche=niche*daysbeforeED_0_001
replace t_gov=gov*daysbeforeED_0_001
replace t_inc=inc*daysbeforeED_0_001 

****************************************************************************************************************

  gen estimate=.
  gen stderror=.
  gen n=.
  gen min95=.
  gen max95=.
  
local agrp "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365"
local bgrp "1.17E-09 0.1419616 0.2248023 0.2834263 0.3287767 0.3657292 0.3968855 0.4237985 0.4474702 0.468585 0.4876307 0.5049679 0.5208701 0.5355504 0.5491773 0.5618868 0.5737901 0.5849797 0.5955325 0.6055138 0.6149793 0.6239773 0.6325493 0.6407312 0.6485552 0.6560493 0.6632382 0.6701443 0.6767874 0.6831853 0.6893542 0.6953083 0.7010612 0.7066249 0.7120099 0.7172266 0.7222844 0.7271914 0.7319558 0.7365844 0.7410842 0.7454613 0.7497215 0.7538702 0.7579125 0.7618527 0.7656955 0.769445 0.7731051 0.7766792 0.7801707 0.7835831 0.7869191 0.7901816 0.7933736 0.7964973 0.7995552 0.8025498 0.805483 0.8083567 0.8111735 0.8139346 0.816642 0.8192976 0.8219028 0.8244591 0.8269681 0.8294312 0.8318496 0.8342248 0.8365577 0.8388499 0.8411022 0.8433158 0.8454917 0.847631 0.8497345 0.8518032 0.853838 0.8558397 0.8578092 0.8597473 0.8616548 0.8635322 0.8653804 0.8672 0.8689917 0.8707563 0.8724942 0.8742059 0.8758923 0.8775536 0.8791909 0.880804 0.8823937 0.8839608 0.8855053 0.887028 0.8885291 0.8900093 0.8914688 0.892908 0.8943275 0.8957275 0.8971082 0.8984703 0.8998142 0.9011399 0.9024481 0.9037387 0.9050125 0.9062693 0.9075098 0.9087342 0.9099426 0.9111354 0.9123128 0.9134752 0.9146227 0.9157558 0.9168745 0.9179791 0.9190698 0.9201468 0.9212106 0.9222609 0.9232984 0.924323 0.925335 0.9263347 0.9273221 0.9282975 0.929261 0.930213 0.9311535 0.9320824 0.9330003 0.9339072 0.9348033 0.9356887 0.9365636 0.9374281 0.9382823 0.9391264 0.9399605 0.940785 0.9415997 0.9424049 0.9432005 0.9439868 0.9447642 0.9455323 0.9462915 0.947042 0.9477836 0.9485168 0.9492414 0.9499577 0.9506657 0.9513656 0.9520574 0.9527411 0.9534172 0.9540852 0.9547458 0.9553988 0.9560441 0.9566822 0.9573128 0.9579363 0.9585527 0.9591619 0.9597642 0.9603597 0.9609482 0.96153 0.9621053 0.9626738 0.9632359 0.9637915 0.9643407 0.9648838 0.9654204 0.9659509 0.9664754 0.9669937 0.9675063 0.9680128 0.9685135 0.9690085 0.9694977 0.9699812 0.9704592 0.9709315 0.9713986 0.97186 0.972316 0.972767 0.9732125 0.9736528 0.9740878 0.9745179 0.9749429 0.9753629 0.9757778 0.976188 0.9765931 0.9769936 0.9773891 0.9777802 0.9781662 0.9785479 0.978925 0.9792972 0.9796653 0.9800287 0.9803878 0.9807425 0.9810928 0.9814386 0.9817805 0.982118 0.9824513 0.9827804 0.9831055 0.9834265 0.9837433 0.9840563 0.9843653 0.9846701 0.9849714 0.9852685 0.985562 0.9858516 0.9861373 0.9864194 0.9866978 0.9869725 0.9872437 0.987511 0.9877748 0.9880353 0.9882919 0.9885452 0.988795 0.9890414 0.9892843 0.9895238 0.98976 0.989993 0.9902225 0.9904488 0.9906718 0.9908916 0.9911083 0.9913216 0.991532 0.9917391 0.9919432 0.9921442 0.9923421 0.992537 0.9927288 0.9929178 0.9931037 0.9932867 0.9934667 0.9936439 0.993818 0.9939895 0.9941583 0.9943241 0.994487 0.9946471 0.9948048 0.9949595 0.9951115 0.9952608 0.9954075 0.9955516 0.9956929 0.9958317 0.9959678 0.9961014 0.9962325 0.996361 0.996487 0.9966103 0.9967313 0.9968498 0.9969658 0.9970796 0.9971907 0.9972994 0.9974059 0.9975098 0.9976115 0.9977108 0.9978077 0.9979025 0.9979948 0.9980849 0.9981728 0.9982582 0.9983416 0.9984228 0.9985017 0.9985784 0.9986529 0.9987254 0.9987957 0.9988637 0.9989297 0.9989935 0.9990553 0.999115 0.9991725 0.9992282 0.9992816 0.9993331 0.9993827 0.9994302 0.9994757 0.9995192 0.9995607 0.9996003 0.999638 0.9996735 0.9997075 0.9997393 0.9997692 0.9997973 0.9998235 0.9998479 0.9998704 0.999891 0.9999099 0.9999269 0.9999422 0.9999555 0.9999671 0.9999769 0.9999851 0.9999914 0.9999959 0.9999987 0.9999997 0.9999993 0.999997 0.9999928 0.9999872 0.9999799 0.9999709 0.9999602 0.9999478 0.9999338 0.9999181 0.9999009 0.999882 0.9998615 0.9998395 0.9998159 0.9997908 0.999764 0.9997357"

local n : word count `agrp'

mi estimate, post dots: reg log_ae daysbeforeED_0_001 ipollse enpp t_enpp pr t_pr vote t_vote partyold t_partyold niche t_niche gov t_gov inc t_inc

forvalues i = 1/`n' {
  local a : word `i' of `agrp'
  local b : word `i' of `bgrp'
  
{
  nlcom (_b[partyold])+(_b[t_partyold]*(`b'))

  matrix b = r(b)
  scalar bt = b[1,1]
  replace estimate=bt in `a'
  matrix b = r(V)
  scalar bt = b[1,1]
  replace stderror=sqrt(bt) in `a'
  replace n=`a' in `a'
}
}

replace min95=estimate-stderror
replace max95=estimate+stderror

keep n estimate min95 max95
drop if estimate==.
  
gen count=_n

format estimate min95 max95 %8.1f

rename estimate ae
rename min95 ae_min95
rename max95 ae_max95

sort count

gen aeraw=exp(ae)
replace aeraw=aeraw-1

gen aerawmin95=exp(ae_min95)
replace aerawmin95=aerawmin95-1

gen aeraw_max95=exp(ae_max95)
replace aeraw_max95=aeraw_max95-1

save MarginsNL_partyage.dta, replace

use MarginsNL_partyage.dta, clear

* 
twoway line aeraw count, /*
*/ clpattern(solid) clcolor(gs0) clwidth(medthick)  /* 
*/ || line aerawmin95 count,  /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/ || line aeraw_max95 count, /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/  , scheme(plottig) graphregion(color(white)) /*
*/  xtitle("Days until election") /*
*/  title("Party age", size(small)) /*
*/  ylabel(-0.2(0.1).2, labsize(small) gmax angle(horizontal)) /*
*/  ytick(-0.2(0.1).2) /*
*/  yscale(r(-.2 .2) titlegap(*5)) /*
*/  yline(0, lcolor(red) lpattern(solid)) /*
*/  xlabel(0(100)400, labsize(small)) /*
*/  xscale(reverse) xscale(titlegap(3)) /*
*/  legend(off)  /*
*/  saving(MarginsNL_4_partyage.gph, replace)


*************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************

clear
set matsize 2000

*************************************************************************************************************************************************************************
**** --> MARGINAL EFFECTS - STANDARDISED INDEPENDENT VARIABLES **********************************************************************************************************
*************************************************************************************************************************************************************************

****************************************************************************************************************
**** PARTY-LEVEL MODEL: MI DATA + PARTY CONTROLS ***************************************************************
****************************************************************************************************************

use TimelinePartiesMI_New.dta, clear

*** STANDARDISE THE INDEPENDENT VARIABLES **********************************************************************

egen std_pr=std(pr)
egen std_enpp=std(enpp)
egen std_partycentric=std(partycentric)
egen std_vote=std(vote_)
egen std_partyold=std(partyold)
egen std_niche=std(niche)
egen std_gov=std(gov_)
egen std_inc=std(inc_)

replace pr=std_pr
replace enpp=std_enpp
replace partycentric=std_partycentric 
replace vote=std_vote 	
replace partyold=std_partyold
replace niche=std_niche 
replace gov=std_gov 	
replace inc=std_inc 	

replace t_pr=pr*daysbeforeED_0_001
replace t_enpp=enpp*daysbeforeED_0_001
replace t_partycentric=partycentric*daysbeforeED_0_001
replace t_vote=vote*daysbeforeED_0_001
replace t_partyold=partyold*daysbeforeED_0_001 
replace t_niche=niche*daysbeforeED_0_001
replace t_gov=gov*daysbeforeED_0_001
replace t_inc=inc*daysbeforeED_0_001 

****************************************************************************************************************

  gen estimate=.
  gen stderror=.
  gen n=.
  gen min95=.
  gen max95=.
  
local agrp "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365"
local bgrp "1.17E-09 0.1419616 0.2248023 0.2834263 0.3287767 0.3657292 0.3968855 0.4237985 0.4474702 0.468585 0.4876307 0.5049679 0.5208701 0.5355504 0.5491773 0.5618868 0.5737901 0.5849797 0.5955325 0.6055138 0.6149793 0.6239773 0.6325493 0.6407312 0.6485552 0.6560493 0.6632382 0.6701443 0.6767874 0.6831853 0.6893542 0.6953083 0.7010612 0.7066249 0.7120099 0.7172266 0.7222844 0.7271914 0.7319558 0.7365844 0.7410842 0.7454613 0.7497215 0.7538702 0.7579125 0.7618527 0.7656955 0.769445 0.7731051 0.7766792 0.7801707 0.7835831 0.7869191 0.7901816 0.7933736 0.7964973 0.7995552 0.8025498 0.805483 0.8083567 0.8111735 0.8139346 0.816642 0.8192976 0.8219028 0.8244591 0.8269681 0.8294312 0.8318496 0.8342248 0.8365577 0.8388499 0.8411022 0.8433158 0.8454917 0.847631 0.8497345 0.8518032 0.853838 0.8558397 0.8578092 0.8597473 0.8616548 0.8635322 0.8653804 0.8672 0.8689917 0.8707563 0.8724942 0.8742059 0.8758923 0.8775536 0.8791909 0.880804 0.8823937 0.8839608 0.8855053 0.887028 0.8885291 0.8900093 0.8914688 0.892908 0.8943275 0.8957275 0.8971082 0.8984703 0.8998142 0.9011399 0.9024481 0.9037387 0.9050125 0.9062693 0.9075098 0.9087342 0.9099426 0.9111354 0.9123128 0.9134752 0.9146227 0.9157558 0.9168745 0.9179791 0.9190698 0.9201468 0.9212106 0.9222609 0.9232984 0.924323 0.925335 0.9263347 0.9273221 0.9282975 0.929261 0.930213 0.9311535 0.9320824 0.9330003 0.9339072 0.9348033 0.9356887 0.9365636 0.9374281 0.9382823 0.9391264 0.9399605 0.940785 0.9415997 0.9424049 0.9432005 0.9439868 0.9447642 0.9455323 0.9462915 0.947042 0.9477836 0.9485168 0.9492414 0.9499577 0.9506657 0.9513656 0.9520574 0.9527411 0.9534172 0.9540852 0.9547458 0.9553988 0.9560441 0.9566822 0.9573128 0.9579363 0.9585527 0.9591619 0.9597642 0.9603597 0.9609482 0.96153 0.9621053 0.9626738 0.9632359 0.9637915 0.9643407 0.9648838 0.9654204 0.9659509 0.9664754 0.9669937 0.9675063 0.9680128 0.9685135 0.9690085 0.9694977 0.9699812 0.9704592 0.9709315 0.9713986 0.97186 0.972316 0.972767 0.9732125 0.9736528 0.9740878 0.9745179 0.9749429 0.9753629 0.9757778 0.976188 0.9765931 0.9769936 0.9773891 0.9777802 0.9781662 0.9785479 0.978925 0.9792972 0.9796653 0.9800287 0.9803878 0.9807425 0.9810928 0.9814386 0.9817805 0.982118 0.9824513 0.9827804 0.9831055 0.9834265 0.9837433 0.9840563 0.9843653 0.9846701 0.9849714 0.9852685 0.985562 0.9858516 0.9861373 0.9864194 0.9866978 0.9869725 0.9872437 0.987511 0.9877748 0.9880353 0.9882919 0.9885452 0.988795 0.9890414 0.9892843 0.9895238 0.98976 0.989993 0.9902225 0.9904488 0.9906718 0.9908916 0.9911083 0.9913216 0.991532 0.9917391 0.9919432 0.9921442 0.9923421 0.992537 0.9927288 0.9929178 0.9931037 0.9932867 0.9934667 0.9936439 0.993818 0.9939895 0.9941583 0.9943241 0.994487 0.9946471 0.9948048 0.9949595 0.9951115 0.9952608 0.9954075 0.9955516 0.9956929 0.9958317 0.9959678 0.9961014 0.9962325 0.996361 0.996487 0.9966103 0.9967313 0.9968498 0.9969658 0.9970796 0.9971907 0.9972994 0.9974059 0.9975098 0.9976115 0.9977108 0.9978077 0.9979025 0.9979948 0.9980849 0.9981728 0.9982582 0.9983416 0.9984228 0.9985017 0.9985784 0.9986529 0.9987254 0.9987957 0.9988637 0.9989297 0.9989935 0.9990553 0.999115 0.9991725 0.9992282 0.9992816 0.9993331 0.9993827 0.9994302 0.9994757 0.9995192 0.9995607 0.9996003 0.999638 0.9996735 0.9997075 0.9997393 0.9997692 0.9997973 0.9998235 0.9998479 0.9998704 0.999891 0.9999099 0.9999269 0.9999422 0.9999555 0.9999671 0.9999769 0.9999851 0.9999914 0.9999959 0.9999987 0.9999997 0.9999993 0.999997 0.9999928 0.9999872 0.9999799 0.9999709 0.9999602 0.9999478 0.9999338 0.9999181 0.9999009 0.999882 0.9998615 0.9998395 0.9998159 0.9997908 0.999764 0.9997357"

local n : word count `agrp'

mi estimate, post dots: reg log_ae daysbeforeED_0_001 ipollse enpp t_enpp pr t_pr vote t_vote partyold t_partyold niche t_niche gov t_gov inc t_inc

forvalues i = 1/`n' {
  local a : word `i' of `agrp'
  local b : word `i' of `bgrp'

{
  nlcom (_b[niche])+(_b[t_niche]*(`b'))

  matrix b = r(b)
  scalar bt = b[1,1]
  replace estimate=bt in `a'
  matrix b = r(V)
  scalar bt = b[1,1]
  replace stderror=sqrt(bt) in `a'
  replace n=`a' in `a'
}
}

replace min95=estimate-stderror
replace max95=estimate+stderror

keep n estimate min95 max95
drop if estimate==.
  
gen count=_n

format estimate min95 max95 %8.1f

rename estimate ae
rename min95 ae_min95
rename max95 ae_max95

sort count

gen aeraw=exp(ae)
replace aeraw=aeraw-1

gen aerawmin95=exp(ae_min95)
replace aerawmin95=aerawmin95-1

gen aeraw_max95=exp(ae_max95)
replace aeraw_max95=aeraw_max95-1

save MarginsNL_niche.dta, replace

use MarginsNL_niche.dta, clear

* 
twoway line aeraw count, /*
*/ clpattern(solid) clcolor(gs0) clwidth(medthick)  /* 
*/ || line aerawmin95 count,  /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/ || line aeraw_max95 count, /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/  , scheme(plottig) graphregion(color(white)) /*
*/  xtitle("Days until election") /*
*/  title("Niche parties", size(small)) /*
*/  ylabel(-0.2(0.1).2, labsize(small) gmax angle(horizontal)) /*
*/  ytick(-0.2(0.1).2) /*
*/  yscale(r(-.2 .2) titlegap(*5)) /*
*/  yline(0, lcolor(red) lpattern(solid)) /*
*/  xlabel(0(100)400, labsize(small)) /*
*/  xscale(reverse) xscale(titlegap(3)) /*
*/  legend(off)  /*
*/  saving(MarginsNL_5_niche.gph, replace)


*************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************

clear
set matsize 2000

*************************************************************************************************************************************************************************
**** --> MARGINAL EFFECTS - STANDARDISED INDEPENDENT VARIABLES **********************************************************************************************************
*************************************************************************************************************************************************************************

****************************************************************************************************************
**** PARTY-LEVEL MODEL: MI DATA + PARTY CONTROLS ***************************************************************
****************************************************************************************************************

use TimelinePartiesMI_New.dta, clear

*** STANDARDISE THE INDEPENDENT VARIABLES **********************************************************************

egen std_pr=std(pr)
egen std_enpp=std(enpp)
egen std_partycentric=std(partycentric)
egen std_vote=std(vote_)
egen std_partyold=std(partyold)
egen std_niche=std(niche)
egen std_gov=std(gov_)
egen std_inc=std(inc_)

replace pr=std_pr
replace enpp=std_enpp
replace partycentric=std_partycentric 
replace vote=std_vote 	
replace partyold=std_partyold
replace niche=std_niche 
replace gov=std_gov 	
replace inc=std_inc 	

replace t_pr=pr*daysbeforeED_0_001
replace t_enpp=enpp*daysbeforeED_0_001
replace t_partycentric=partycentric*daysbeforeED_0_001
replace t_vote=vote*daysbeforeED_0_001
replace t_partyold=partyold*daysbeforeED_0_001 
replace t_niche=niche*daysbeforeED_0_001
replace t_gov=gov*daysbeforeED_0_001
replace t_inc=inc*daysbeforeED_0_001 

****************************************************************************************************************

  gen estimate=.
  gen stderror=.
  gen n=.
  gen min95=.
  gen max95=.
  
local agrp "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365"
local bgrp "1.17E-09 0.1419616 0.2248023 0.2834263 0.3287767 0.3657292 0.3968855 0.4237985 0.4474702 0.468585 0.4876307 0.5049679 0.5208701 0.5355504 0.5491773 0.5618868 0.5737901 0.5849797 0.5955325 0.6055138 0.6149793 0.6239773 0.6325493 0.6407312 0.6485552 0.6560493 0.6632382 0.6701443 0.6767874 0.6831853 0.6893542 0.6953083 0.7010612 0.7066249 0.7120099 0.7172266 0.7222844 0.7271914 0.7319558 0.7365844 0.7410842 0.7454613 0.7497215 0.7538702 0.7579125 0.7618527 0.7656955 0.769445 0.7731051 0.7766792 0.7801707 0.7835831 0.7869191 0.7901816 0.7933736 0.7964973 0.7995552 0.8025498 0.805483 0.8083567 0.8111735 0.8139346 0.816642 0.8192976 0.8219028 0.8244591 0.8269681 0.8294312 0.8318496 0.8342248 0.8365577 0.8388499 0.8411022 0.8433158 0.8454917 0.847631 0.8497345 0.8518032 0.853838 0.8558397 0.8578092 0.8597473 0.8616548 0.8635322 0.8653804 0.8672 0.8689917 0.8707563 0.8724942 0.8742059 0.8758923 0.8775536 0.8791909 0.880804 0.8823937 0.8839608 0.8855053 0.887028 0.8885291 0.8900093 0.8914688 0.892908 0.8943275 0.8957275 0.8971082 0.8984703 0.8998142 0.9011399 0.9024481 0.9037387 0.9050125 0.9062693 0.9075098 0.9087342 0.9099426 0.9111354 0.9123128 0.9134752 0.9146227 0.9157558 0.9168745 0.9179791 0.9190698 0.9201468 0.9212106 0.9222609 0.9232984 0.924323 0.925335 0.9263347 0.9273221 0.9282975 0.929261 0.930213 0.9311535 0.9320824 0.9330003 0.9339072 0.9348033 0.9356887 0.9365636 0.9374281 0.9382823 0.9391264 0.9399605 0.940785 0.9415997 0.9424049 0.9432005 0.9439868 0.9447642 0.9455323 0.9462915 0.947042 0.9477836 0.9485168 0.9492414 0.9499577 0.9506657 0.9513656 0.9520574 0.9527411 0.9534172 0.9540852 0.9547458 0.9553988 0.9560441 0.9566822 0.9573128 0.9579363 0.9585527 0.9591619 0.9597642 0.9603597 0.9609482 0.96153 0.9621053 0.9626738 0.9632359 0.9637915 0.9643407 0.9648838 0.9654204 0.9659509 0.9664754 0.9669937 0.9675063 0.9680128 0.9685135 0.9690085 0.9694977 0.9699812 0.9704592 0.9709315 0.9713986 0.97186 0.972316 0.972767 0.9732125 0.9736528 0.9740878 0.9745179 0.9749429 0.9753629 0.9757778 0.976188 0.9765931 0.9769936 0.9773891 0.9777802 0.9781662 0.9785479 0.978925 0.9792972 0.9796653 0.9800287 0.9803878 0.9807425 0.9810928 0.9814386 0.9817805 0.982118 0.9824513 0.9827804 0.9831055 0.9834265 0.9837433 0.9840563 0.9843653 0.9846701 0.9849714 0.9852685 0.985562 0.9858516 0.9861373 0.9864194 0.9866978 0.9869725 0.9872437 0.987511 0.9877748 0.9880353 0.9882919 0.9885452 0.988795 0.9890414 0.9892843 0.9895238 0.98976 0.989993 0.9902225 0.9904488 0.9906718 0.9908916 0.9911083 0.9913216 0.991532 0.9917391 0.9919432 0.9921442 0.9923421 0.992537 0.9927288 0.9929178 0.9931037 0.9932867 0.9934667 0.9936439 0.993818 0.9939895 0.9941583 0.9943241 0.994487 0.9946471 0.9948048 0.9949595 0.9951115 0.9952608 0.9954075 0.9955516 0.9956929 0.9958317 0.9959678 0.9961014 0.9962325 0.996361 0.996487 0.9966103 0.9967313 0.9968498 0.9969658 0.9970796 0.9971907 0.9972994 0.9974059 0.9975098 0.9976115 0.9977108 0.9978077 0.9979025 0.9979948 0.9980849 0.9981728 0.9982582 0.9983416 0.9984228 0.9985017 0.9985784 0.9986529 0.9987254 0.9987957 0.9988637 0.9989297 0.9989935 0.9990553 0.999115 0.9991725 0.9992282 0.9992816 0.9993331 0.9993827 0.9994302 0.9994757 0.9995192 0.9995607 0.9996003 0.999638 0.9996735 0.9997075 0.9997393 0.9997692 0.9997973 0.9998235 0.9998479 0.9998704 0.999891 0.9999099 0.9999269 0.9999422 0.9999555 0.9999671 0.9999769 0.9999851 0.9999914 0.9999959 0.9999987 0.9999997 0.9999993 0.999997 0.9999928 0.9999872 0.9999799 0.9999709 0.9999602 0.9999478 0.9999338 0.9999181 0.9999009 0.999882 0.9998615 0.9998395 0.9998159 0.9997908 0.999764 0.9997357"

local n : word count `agrp'

mi estimate, post dots: reg log_ae daysbeforeED_0_001 ipollse enpp t_enpp pr t_pr vote t_vote partyold t_partyold niche t_niche gov t_gov inc t_inc

forvalues i = 1/`n' {
  local a : word `i' of `agrp'
  local b : word `i' of `bgrp'

{
  nlcom (_b[gov])+(_b[t_gov]*(`b'))

  matrix b = r(b)
  scalar bt = b[1,1]
  replace estimate=bt in `a'
  matrix b = r(V)
  scalar bt = b[1,1]
  replace stderror=sqrt(bt) in `a'
  replace n=`a' in `a'
}
}

replace min95=estimate-stderror
replace max95=estimate+stderror

keep n estimate min95 max95
drop if estimate==.
  
gen count=_n

format estimate min95 max95 %8.1f

rename estimate ae
rename min95 ae_min95
rename max95 ae_max95

sort count

gen aeraw=exp(ae)
replace aeraw=aeraw-1

gen aerawmin95=exp(ae_min95)
replace aerawmin95=aerawmin95-1

gen aeraw_max95=exp(ae_max95)
replace aeraw_max95=aeraw_max95-1

save MarginsNL_gov.dta, replace

use MarginsNL_gov.dta, clear

* 
twoway line aeraw count, /*
*/ clpattern(solid) clcolor(gs0) clwidth(medthick)  /* 
*/ || line aerawmin95 count,  /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/ || line aeraw_max95 count, /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/  , scheme(plottig) graphregion(color(white)) /*
*/  xtitle("Days until election") /*
*/  title("Governing parties", size(small)) /*
*/  ylabel(-0.2(0.1).2, labsize(small) gmax angle(horizontal)) /*
*/  ytick(-0.2(0.1).2) /*
*/  yscale(r(-.2 .2) titlegap(*5)) /*
*/  yline(0, lcolor(red) lpattern(solid)) /*
*/  xlabel(0(100)400, labsize(small)) /*
*/  xscale(reverse) xscale(titlegap(3)) /*
*/  legend(off)  /*
*/  saving(MarginsNL_6_gov.gph, replace)


*************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************

clear
set matsize 2000

*************************************************************************************************************************************************************************
**** --> MARGINAL EFFECTS - STANDARDISED INDEPENDENT VARIABLES **********************************************************************************************************
*************************************************************************************************************************************************************************

****************************************************************************************************************
**** PARTY-LEVEL MODEL: MI DATA + PARTY CONTROLS ***************************************************************
****************************************************************************************************************

use TimelinePartiesMI_New.dta, clear

*** STANDARDISE THE INDEPENDENT VARIABLES **********************************************************************

egen std_pr=std(pr)
egen std_enpp=std(enpp)
egen std_partycentric=std(partycentric)
egen std_vote=std(vote_)
egen std_partyold=std(partyold)
egen std_niche=std(niche)
egen std_gov=std(gov_)
egen std_inc=std(inc_)

replace pr=std_pr
replace enpp=std_enpp
replace partycentric=std_partycentric 
replace vote=std_vote 	
replace partyold=std_partyold
replace niche=std_niche 
replace gov=std_gov 	
replace inc=std_inc 	

replace t_pr=pr*daysbeforeED_0_001
replace t_enpp=enpp*daysbeforeED_0_001
replace t_partycentric=partycentric*daysbeforeED_0_001
replace t_vote=vote*daysbeforeED_0_001
replace t_partyold=partyold*daysbeforeED_0_001 
replace t_niche=niche*daysbeforeED_0_001
replace t_gov=gov*daysbeforeED_0_001
replace t_inc=inc*daysbeforeED_0_001 

****************************************************************************************************************

  gen estimate=.
  gen stderror=.
  gen n=.
  gen min95=.
  gen max95=. 
  
local agrp "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365"
local bgrp "1.17E-09 0.1419616 0.2248023 0.2834263 0.3287767 0.3657292 0.3968855 0.4237985 0.4474702 0.468585 0.4876307 0.5049679 0.5208701 0.5355504 0.5491773 0.5618868 0.5737901 0.5849797 0.5955325 0.6055138 0.6149793 0.6239773 0.6325493 0.6407312 0.6485552 0.6560493 0.6632382 0.6701443 0.6767874 0.6831853 0.6893542 0.6953083 0.7010612 0.7066249 0.7120099 0.7172266 0.7222844 0.7271914 0.7319558 0.7365844 0.7410842 0.7454613 0.7497215 0.7538702 0.7579125 0.7618527 0.7656955 0.769445 0.7731051 0.7766792 0.7801707 0.7835831 0.7869191 0.7901816 0.7933736 0.7964973 0.7995552 0.8025498 0.805483 0.8083567 0.8111735 0.8139346 0.816642 0.8192976 0.8219028 0.8244591 0.8269681 0.8294312 0.8318496 0.8342248 0.8365577 0.8388499 0.8411022 0.8433158 0.8454917 0.847631 0.8497345 0.8518032 0.853838 0.8558397 0.8578092 0.8597473 0.8616548 0.8635322 0.8653804 0.8672 0.8689917 0.8707563 0.8724942 0.8742059 0.8758923 0.8775536 0.8791909 0.880804 0.8823937 0.8839608 0.8855053 0.887028 0.8885291 0.8900093 0.8914688 0.892908 0.8943275 0.8957275 0.8971082 0.8984703 0.8998142 0.9011399 0.9024481 0.9037387 0.9050125 0.9062693 0.9075098 0.9087342 0.9099426 0.9111354 0.9123128 0.9134752 0.9146227 0.9157558 0.9168745 0.9179791 0.9190698 0.9201468 0.9212106 0.9222609 0.9232984 0.924323 0.925335 0.9263347 0.9273221 0.9282975 0.929261 0.930213 0.9311535 0.9320824 0.9330003 0.9339072 0.9348033 0.9356887 0.9365636 0.9374281 0.9382823 0.9391264 0.9399605 0.940785 0.9415997 0.9424049 0.9432005 0.9439868 0.9447642 0.9455323 0.9462915 0.947042 0.9477836 0.9485168 0.9492414 0.9499577 0.9506657 0.9513656 0.9520574 0.9527411 0.9534172 0.9540852 0.9547458 0.9553988 0.9560441 0.9566822 0.9573128 0.9579363 0.9585527 0.9591619 0.9597642 0.9603597 0.9609482 0.96153 0.9621053 0.9626738 0.9632359 0.9637915 0.9643407 0.9648838 0.9654204 0.9659509 0.9664754 0.9669937 0.9675063 0.9680128 0.9685135 0.9690085 0.9694977 0.9699812 0.9704592 0.9709315 0.9713986 0.97186 0.972316 0.972767 0.9732125 0.9736528 0.9740878 0.9745179 0.9749429 0.9753629 0.9757778 0.976188 0.9765931 0.9769936 0.9773891 0.9777802 0.9781662 0.9785479 0.978925 0.9792972 0.9796653 0.9800287 0.9803878 0.9807425 0.9810928 0.9814386 0.9817805 0.982118 0.9824513 0.9827804 0.9831055 0.9834265 0.9837433 0.9840563 0.9843653 0.9846701 0.9849714 0.9852685 0.985562 0.9858516 0.9861373 0.9864194 0.9866978 0.9869725 0.9872437 0.987511 0.9877748 0.9880353 0.9882919 0.9885452 0.988795 0.9890414 0.9892843 0.9895238 0.98976 0.989993 0.9902225 0.9904488 0.9906718 0.9908916 0.9911083 0.9913216 0.991532 0.9917391 0.9919432 0.9921442 0.9923421 0.992537 0.9927288 0.9929178 0.9931037 0.9932867 0.9934667 0.9936439 0.993818 0.9939895 0.9941583 0.9943241 0.994487 0.9946471 0.9948048 0.9949595 0.9951115 0.9952608 0.9954075 0.9955516 0.9956929 0.9958317 0.9959678 0.9961014 0.9962325 0.996361 0.996487 0.9966103 0.9967313 0.9968498 0.9969658 0.9970796 0.9971907 0.9972994 0.9974059 0.9975098 0.9976115 0.9977108 0.9978077 0.9979025 0.9979948 0.9980849 0.9981728 0.9982582 0.9983416 0.9984228 0.9985017 0.9985784 0.9986529 0.9987254 0.9987957 0.9988637 0.9989297 0.9989935 0.9990553 0.999115 0.9991725 0.9992282 0.9992816 0.9993331 0.9993827 0.9994302 0.9994757 0.9995192 0.9995607 0.9996003 0.999638 0.9996735 0.9997075 0.9997393 0.9997692 0.9997973 0.9998235 0.9998479 0.9998704 0.999891 0.9999099 0.9999269 0.9999422 0.9999555 0.9999671 0.9999769 0.9999851 0.9999914 0.9999959 0.9999987 0.9999997 0.9999993 0.999997 0.9999928 0.9999872 0.9999799 0.9999709 0.9999602 0.9999478 0.9999338 0.9999181 0.9999009 0.999882 0.9998615 0.9998395 0.9998159 0.9997908 0.999764 0.9997357"

local n : word count `agrp'

mi estimate, post dots: reg log_ae daysbeforeED_0_001 ipollse enpp t_enpp pr t_pr vote t_vote partyold t_partyold niche t_niche gov t_gov inc t_inc
  
forvalues i = 1/`n' {
  local a : word `i' of `agrp'
  local b : word `i' of `bgrp'

{
  nlcom (_b[inc])+(_b[t_inc]*(`b'))

  matrix b = r(b)
  scalar bt = b[1,1]
  replace estimate=bt in `a'
  matrix b = r(V)
  scalar bt = b[1,1]
  replace stderror=sqrt(bt) in `a'
  replace n=`a' in `a'
}
}

replace min95=estimate-stderror
replace max95=estimate+stderror

keep n estimate min95 max95
drop if estimate==.
  
gen count=_n

format estimate min95 max95 %8.1f

rename estimate ae
rename min95 ae_min95
rename max95 ae_max95

sort count

gen aeraw=exp(ae)
replace aeraw=aeraw-1

gen aerawmin95=exp(ae_min95)
replace aerawmin95=aerawmin95-1

gen aeraw_max95=exp(ae_max95)
replace aeraw_max95=aeraw_max95-1

save MarginsNL_inc.dta, replace

use MarginsNL_inc.dta, clear

* 
twoway line aeraw count, /*
*/ clpattern(solid) clcolor(gs0) clwidth(medthick)  /* 
*/ || line aerawmin95 count,  /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/ || line aeraw_max95 count, /*
*/ clpattern(shortdash) clcolor(gs0) clwidth(medthin) /*
*/  , scheme(plottig) graphregion(color(white)) /*
*/  xtitle("Days until election") /*
*/  title("Main governing parties", size(small)) /*
*/  ylabel(-0.2(0.1).2, labsize(small) gmax angle(horizontal)) /*
*/  ytick(-0.2(0.1).2) /*
*/  yscale(r(-.2 .2) titlegap(*5)) /*
*/  yline(0, lcolor(red) lpattern(solid)) /*
*/  xlabel(0(100)400, labsize(small)) /*
*/  xscale(reverse) xscale(titlegap(3)) /*
*/  legend(off)  /*
*/  saving(MarginsNL_7_inc.gph, replace)


graph combine MarginsNL_2_pr.gph MarginsNL_1_enpp.gph MarginsNL_3_partysize.gph MarginsNL_4_partyage.gph MarginsNL_5_niche.gph MarginsNL_6_gov.gph MarginsNL_7_inc.gph, rows(3) ysize(16) xsize(16) graphregion(color(white)) title("") imargin(2 2 2 2 2)

graph export EJPR_Fig2.png, width(4000) replace
graph export EJPR_Fig2.tif, width(2000) replace
